package com.bulletelc.apollopre.module.main.center.address.list;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.constants.IntentCode;
import com.bulletelc.apollopre.entity.AddressDetailBean;
import com.bulletelc.apollopre.module.main.center.address.edit.EditAddressActivity;

import java.util.List;

/**
 * author: lhl
 * date  : 2018/01/23
 * desc  : 地址列表的adapter
 */

public class AddressListAdapter extends RecyclerView.Adapter<AddressListAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<AddressDetailBean> addressList;
    private boolean isChoose;
    private AddressContract.Presenter presenter;
    private LayoutInflater inflater;
    private OnCheckboxClickListener onCheckboxClickListener;
    private OnCardViewClickListener onCardViewClickListener;

    public AddressListAdapter(Context context, List<AddressDetailBean> addressDetailBeans, boolean isChoose, AddressContract.Presenter presenter) {
        this.context = context;
        this.addressList = addressDetailBeans;
        this.isChoose = isChoose;
        this.presenter = presenter;
        inflater = LayoutInflater.from(context);
    }

    public void setOnCheckboxClickListener(OnCheckboxClickListener onCheckboxClickListener) {
        this.onCheckboxClickListener = onCheckboxClickListener;
    }

    public void setOnCardViewClickListener(OnCardViewClickListener onCardViewClickListener) {
        this.onCardViewClickListener = onCardViewClickListener;
    }

    @Override
    public void onClick(View v) {
        final int position = (int) v.getTag();
        switch (v.getId()) {
            case R.id.cardView://点击整个cardView
                if (isChoose) {
                    if (onCardViewClickListener != null) {
                        onCardViewClickListener.onCardViewClick(v, position);
                    }
                }
                break;

            case R.id.cbDefault:
                if (onCheckboxClickListener != null) {
                    onCheckboxClickListener.onCheckboxClick(position);
                }
                break;

            case R.id.tvEdit:
                Intent intent = new Intent(context, EditAddressActivity.class);
                intent.putExtra(IntentCode.EXTRA_KEY_ADDRESS_ID, addressList.get(position).getId());
                ((Activity) context).startActivityForResult(intent, IntentCode.REQUEST_CODE_ADDRESS_LIST_TO_ADD);
                break;

            case R.id.tvRemove:
                AlertDialog dialog = new AlertDialog.Builder(context)
                        .setMessage("确定删除地址吗？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                presenter.deleteAddress(addressList.get(position).getId(),position);
                            }
                        })
                        .setNegativeButton("取消",null)
                        .create();
                dialog.show();
                break;
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(inflater.inflate(R.layout.address_list_item, parent, false));
        viewHolder.cardView.setOnClickListener(this);
        viewHolder.checkBox.setOnClickListener(this);
        viewHolder.edit.setOnClickListener(this);
        viewHolder.remove.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        AddressDetailBean address = addressList.get(position);
        holder.name.setText(address.getUserName());
        holder.phone.setText(address.getPhone_no());
        holder.address.setText(address.getRegion_label()+" "+address.getApartment_address());

        holder.checkBox.setTag(position);
        holder.edit.setTag(position);
        holder.remove.setTag(position);
        holder.cardView.setTag(position);

        if (isChoose) {
            holder.rlEditAddress.setVisibility(View.GONE);
        } else {
            holder.checkBox.setChecked(isDefault(address.getIs_default()));
            holder.rlEditAddress.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public int getItemCount() {
        return addressList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout cardView;
        TextView name, phone, address, edit, remove;
        RelativeLayout rlEditAddress;
        CheckBox checkBox;

        public ViewHolder(View itemView) {
            super(itemView);
            cardView = itemView.findViewById(R.id.cardView);
            name = itemView.findViewById(R.id.tvName);
            phone = itemView.findViewById(R.id.tvPhone);
            address = itemView.findViewById(R.id.tvAddress);
            edit = itemView.findViewById(R.id.tvEdit);
            remove = itemView.findViewById(R.id.tvRemove);
            checkBox = itemView.findViewById(R.id.cbDefault);
            rlEditAddress = itemView.findViewById(R.id.rlEditAddress);
        }
    }

    /**
     * 判断是否默认选中
     * @param isDefault
     * @return
     */
    private boolean isDefault(int isDefault) {
        return isDefault == 1 ? true : false;
    }

    public interface OnCheckboxClickListener{
        void onCheckboxClick(int position);
    }

    public interface OnCardViewClickListener {
        void onCardViewClick(View view, int position);
    }
}
